<?php

namespace App\depository;

use App\Enum\PermissionsEnum;
use App\Models\PermissionsModel;

class PermissionsDep
{
    public $model;

    public function __construct(){
        $this->model = new PermissionsModel();
    }

    public function add($data){
        $res = $this->model->insert($data);
        return $res;
    }

    public function getByPid($pid,$type){
        return $this->model->where('parent_id',$pid)->where('type',$type)->get();
    }

    public function addGetId($data){
        $res = $this->model->insertGetId($data);
        return $res;
    }

    public function upd($id,$data){
        $res = $this->model->where('id',$id)->update($data);
        return $res;
    }

    public function del($id){
        $res = $this->model->where('id',$id)->delete();
        return $res;
    }

    public function normal(){
        $res = $this->model->where('status',PermissionsEnum::STATUS_NORMAL)->get();
        return $res;
    }

    public function getChildren($id){
        $res = $this->model->where('parent_id',$id)->get();
        return $res;
    }

    public function firstByGuardName($guardName){
        $res = $this->model->where('guard_name',$guardName)->first();
        return $res;
    }

    public function getPagePermission($permissionIds){
        $res = $this->model->whereIn('id',$permissionIds)->where('type',PermissionsEnum::TYPE_MENU)->where('status',PermissionsEnum::STATUS_NORMAL)->get();
        return $res;
    }

    public function getButtonPermissions($permissionIds){
        $res = $this->model->whereIn('id',$permissionIds)->where('type',PermissionsEnum::TYPE_BUTTON)->where('status',PermissionsEnum::STATUS_NORMAL)->get();
        return $res;
    }
}
